#ifndef NODE_MANAGER_H
#define NODE_MANAGER_H

#include "ns3/node-container.h"
#include "ns3/mobility-model.h"
#include "ns3/internet-stack-helper.h"
#include "propagation-models.h"

#include <string>
#include <vector>
#include <map>
#include <set>

namespace ns3 {

// 定义节点类型
enum NodeType {
    VEHICLE,
    SATELLITE,
    GROUND_STATION
};

// 车辆数据结构
struct VehicleData {
    std::string id;
    double time;
    double x;
    double y;
    double speed;
    double angle;
};

// 位置数据结构
struct PositionData {
    std::string id;
    double x;
    double y;
    double z;
};

class NodeManager
{
public:
    NodeManager();
    virtual ~NodeManager();

    // 从文件加载数据
    void LoadVehicles(const std::string& dirPath);
    void LoadSatellites(const std::string& filePath);
    void LoadGroundStations(const std::string& filePath);

    // 创建节点
    void CreateNodes();

    // 为节点安装Internet协议栈
    void InstallInternetStack();

    // 为节点安装应用程序
    void InstallApplications();

    // 获取节点
    NodeContainer GetAllNodes() const;
    NodeContainer GetVehicleNodes() const;
    NodeContainer GetSatelliteNodes() const;
    NodeContainer GetGroundStationNodes() const;

    // 计算两个节点之间的距离
    double GetDistance(uint32_t node1Id, uint32_t node2Id) const;

    // 获取节点类型
    NodeType GetNodeType(uint32_t nodeId) const;

    // 获取特定时间的车辆数据
    std::vector<VehicleData> GetVehicleDataAtTime(double time) const;

    // 设置传播模型参数
    void SetGroundStationCoverageRadius(double radius);
    void SetSatelliteMaxElevationAngle(double angle);
    void SetupPropagationModels();
    
    // 计算SINR和干扰
    double CalculateGroundStationSINR(uint32_t vehicleNodeId, uint32_t stationNodeId) const;
    double CalculateSatelliteSINR(uint32_t vehicleNodeId, uint32_t satelliteNodeId) const;
    
    // 获取负载信息
    double GetGroundStationLoad(uint32_t stationNodeId) const;
    double GetSatelliteLoad(uint32_t satelliteNodeId) const;

    // 获取节点对应的车辆ID
    std::string GetVehicleIdForNode(uint32_t nodeId) const;

    // 获取传播模型
    Ptr<PropagationLossModel> GetGroundStationPropagationModel() const { return m_groundStationPropagationModel; }
    Ptr<PropagationLossModel> GetSatellitePropagationModel() const { return m_satellitePropagationModel; }
    
    // 添加新方法: 从CSV文件直接添加地面站
    void AddGroundStations(const std::string& positionFile, uint32_t count);
    
private:
    // 解析CSV文件
    std::vector<VehicleData> ParseVehicleCSV(const std::string& filePath, const std::string& vehicleId);
    std::vector<PositionData> ParsePositionCSV(const std::string& filePath);

    // 设置移动模型
    void SetupVehicleMobility();
    void SetupSatelliteMobility();
    void SetupGroundStationMobility();

    // 节点
    NodeContainer m_allNodes;
    NodeContainer m_vehicleNodes;
    NodeContainer m_satelliteNodes;
    NodeContainer m_groundStationNodes;

    // 数据
    std::map<std::string, std::vector<VehicleData>> m_vehicleTrajectories; // 每个车辆ID对应的轨迹
    std::vector<PositionData> m_satellitePositions;
    std::vector<PositionData> m_groundStationPositions;

    // 节点类型映射
    std::map<uint32_t, NodeType> m_nodeTypes;
    
    // 节点ID到车辆ID的映射
    std::map<uint32_t, std::string> m_nodeToVehicleId;

    // 传播模型
    Ptr<PropagationLossModel> m_groundStationPropagationModel;
    Ptr<PropagationLossModel> m_satellitePropagationModel;
    
    // 覆盖范围参数
    double m_groundStationCoverageRadius;
    double m_satelliteMaxElevationAngle;
    
    // 负载记录 (节点ID -> 当前连接的车辆数)
    std::map<uint32_t, uint32_t> m_nodeLoads;
};

} // namespace ns3

#endif /* NODE_MANAGER_H */
